<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="receiverId">
      <el-input v-model="dataForm.receiverId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="收货名" prop="receiverName">
      <el-input v-model="dataForm.receiverName" placeholder="收货名"></el-input>
    </el-form-item>
    <el-form-item label="收货电话" prop="receiverPhone">
      <el-input v-model="dataForm.receiverPhone" placeholder="收货电话"></el-input>
    </el-form-item>
    <el-form-item label="收货地址" prop="receiverDetailAddr">
      <el-input v-model="dataForm.receiverDetailAddr" placeholder="收货地址"></el-input>
    </el-form-item>
    <el-form-item label="[1为默认地址]" prop="status">
      <el-input v-model="dataForm.status" placeholder="[1为默认地址]"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="gmtCreate">
      <el-date-picker
      v-model="dataForm.gmtCreate"
      type="datetime"
      placeholder="创建日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="修改日期" prop="gmtModified">
       <el-date-picker
      v-model="dataForm.gmtModified"
      type="datetime"
      placeholder="修改日期">
    </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          receiverId: '',
          receiverName: '',
          receiverPhone: '',
          receiverDetailAddr: '',
          status: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          receiverId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          receiverName: [
            { required: true, message: '收货名不能为空', trigger: 'blur' }
          ],
          receiverPhone: [
            { required: true, message: '收货电话不能为空', trigger: 'blur' }
          ],
          receiverDetailAddr: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '[1为默认地址]不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '修改日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/address/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.receiverId = data.address.receiverId
                this.dataForm.receiverName = data.address.receiverName
                this.dataForm.receiverPhone = data.address.receiverPhone
                this.dataForm.receiverDetailAddr = data.address.receiverDetailAddr
                this.dataForm.status = data.address.status
                this.dataForm.gmtCreate = data.address.gmtCreate
                this.dataForm.gmtModified = data.address.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cloudgallery/address/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'receiverId': this.dataForm.receiverId,
                'receiverName': this.dataForm.receiverName,
                'receiverPhone': this.dataForm.receiverPhone,
                'receiverDetailAddr': this.dataForm.receiverDetailAddr,
                'status': this.dataForm.status,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
